package com.nari.ies.ne.nw.server.service.fileScore.impl;

import com.nari.ies.ne.nw.server.entity.WindDataScoreEntity;
import com.nari.ies.ne.nw.server.mapper.FileScoreMapper;
import com.nari.ies.ne.nw.server.service.fileScore.IFileScoreService;
import com.nari.ies.ne.nw.server.service.plantDataScore.IWindPlantDataScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FileScoreServiceImpl implements IFileScoreService {

    @Autowired
    private FileScoreMapper fileScoreMapper;

    @Autowired
    private IWindPlantDataScoreService windPlantDataScoreService;


    @Override
    public float getFileDataScore(String plantId,String fileType,String plantType,Date startDate,Date endDate,String tableName){

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);


        //文件数据项总得分
        float fileDataScore = 0;
        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            dataNum =0;
            fileDataScore = 0;
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum = Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum = Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum = Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) / dataNum;

                fileDataScore += dataScore;
            }
        }
        return fileDataScore;
    }
}
